using Main.Details;
using Main.Event;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using ZSW.Framework.UIElement;
using ZSW.Framework.Utils;

namespace Main.UI
{
    public class UI_Slot_Bag : ZSWF_BaseUIElement, IPointerDownHandler,
    IBeginDragHandler, IDragHandler, IEndDragHandler
    {
        [Header("UI绑定")]
        [SerializeField] private Image slotImage;
        [SerializeField] private TextMeshProUGUI amountText;
        [SerializeField] private Image highlightImage;
        [SerializeField] private Button button;
        [Header("是否启用提示栏")]
        [SerializeField] private bool usingToolTip;
        [Header("格子类型")]
        public SlotType slotType;
        public bool isSelect = false;
        public int slotIndex;

        public ItemDetails CurrentItemDetails;
        private int ItemAmount;

        public UI_InventorySystem inventoryUI;

        public override void Init()
        {
            base.Init();
            MainEventSystem.InitFinish += ClearSlot;
        }

        public void ClearSlot()
        {
            // Debug.Log("ClearSlot");
            CurrentItemDetails = null;
            ItemAmount = -1;
            SetEmptySlot();
            MainEventSystem.CallItemSelectEvent(this.CurrentItemDetails, isSelect);
        }

        public void SetInventoryUI(UI_InventorySystem inventoryUI)
        {
            this.inventoryUI = inventoryUI;
            if (usingToolTip)
            {
                UI_ToolTip_Show showTip = this.gameObject.AddComponent<UI_ToolTip_Show>();
                showTip.Init();
                showTip.SetInventoryUI(inventoryUI);
            }
        }

        public void SetItemData(ItemDetails itemDetails, int ItemAmount)
        {
            CurrentItemDetails = itemDetails;
            this.ItemAmount = ItemAmount;
            SetSlot();
        }

        private void SetSlot()
        {
            slotImage.enabled = true;
            slotImage.sprite = CurrentItemDetails.ItemIcon;
            amountText.text = ItemAmount.ToString();
            button.interactable = true;
        }

        private void SetEmptySlot()
        {
            slotImage.enabled = false;
            highlightImage.SetActive(false);
            isSelect = false;
            amountText.text = "";
            button.interactable = false;
        }

        public void SwitchHighLight()
        {
            isSelect = !isSelect;

            if (slotType == SlotType.Bag)
            {
                MainEventSystem.CallItemSelectEvent(this.CurrentItemDetails, isSelect);
            }

            highlightImage.SetActive(isSelect);
        }

        public void DisableHighLight()
        {
            isSelect = false;
            highlightImage.SetActive(isSelect);
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            if (CurrentItemDetails != null && slotType == SlotType.Bag)
            {
                inventoryUI.OnSlotClick(this.slotIndex);
            }
        }

        public void OnBeginDrag(PointerEventData eventData)
        {
            if (CurrentItemDetails != null)
            {
                // inventoryUI.OnSlotClick(this.slotIndex);
                inventoryUI.OnSlotBeginDrag(this.slotImage.sprite);
            }
        }

        public void OnDrag(PointerEventData eventData)
        {
            if (CurrentItemDetails != null)
            {
                inventoryUI.OnSlotDrag(eventData.position);
            }
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            if (CurrentItemDetails != null)
            {
                inventoryUI.OnSlotEndDrag(this.slotType, this.slotIndex, eventData.pointerCurrentRaycast);
                // inventoryUI.OnSlotDrag(eventData.position);
                // Debug.Log(eventData.pointerCurrentRaycast);

                inventoryUI.OnSlotClick(-1);
            }
        }
    }
}

